//
//  XJCommunitySectionHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/31.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

//名字为什么起那么长，要多长有多长
#define kXJCommunitySectionHeaderViewButtonFont [UIFont systemFontOfSize:20.0]

#import "XJCommunitySectionHeaderView.h"
@interface XJCommunitySectionHeaderView()
@property(nonatomic, strong) UIView *bgView;

@property(nonatomic, strong) UIButton *firstButton;
@property(nonatomic, strong) UIButton *secondButton;
//@property(nonatomic, strong) UIView *yellowLineView; // 线
@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;



/**第一个按钮的文字的颜色:想要动态的计算 button 的宽度*/
@property(nonatomic, copy) NSString *firstStr;
@property(nonatomic, copy) NSString *secondStr;

@property (nonatomic, assign) BOOL isHide;

@end

// 区头
@implementation XJCommunitySectionHeaderView
#pragma mark - 一初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.bgView];
    
    [self.bgView addSubview:self.firstButton];
    
    [self.bgView addSubview:self.secondButton];
    
//    CGRect frame = self.frame;
//    self.yellowLineView.frame = CGRectMake(0,frame.size.height - 10 , 20, 2);
//    [self.bgView addSubview:self.yellowLineView];
    
    [self.bgView addSubview:self.line1];
    [self.bgView addSubview:self.line2];
}


#pragma mark - 二 lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
    }
    return _bgView;
}

- (UIButton *)firstButton {
    if (!_firstButton) {
        _firstButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [_firstButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_firstButton addTarget:self action:@selector(clickFirstButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _firstButton;
}

- (UIButton *)secondButton {
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [_secondButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_secondButton addTarget:self action:@selector(clickSecondButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _secondButton;
}

//- (UIView *)yellowLineView {
//    if (!_yellowLineView) {
//        _yellowLineView = [UIView new];
//        _yellowLineView.backgroundColor = [UIColor yellowColor];
//    }
//    return _yellowLineView;
//}

- (UIView *)line1 {
    if (!_line1) {
        _line1 = [UIView new];
        _line1.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    }
    return _line1;
}
- (UIView *)line2 {
    if (!_line2) {
        _line2 = [UIView new];
        _line2.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    }
    return _line2;
}
#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
   
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.firstButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(20);
        make.size.mas_equalTo(CGSizeMake(80, 40));
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(-10);
    }];
 
    [self.secondButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(120);
        make.size.mas_equalTo(CGSizeMake(80, 40));
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(-10);
    }];
    
    
    //线1
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 2));
        make.bottom.equalTo(self.firstButton.mas_bottom).offset(-4);
        make.centerX.equalTo(self.firstButton.mas_centerX);
    }];
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 2));
        make.bottom.equalTo(self.secondButton.mas_bottom).offset(-4);
        make.centerX.equalTo(self.secondButton.mas_centerX);
    }];
    
}
#pragma mark - 四 配置  &  事件回调

- (void)configureXJCommunitySectionHeaderViewHideSecondButton:(BOOL)hide WithFirsttitle:(NSString *)firstTitle secondTitle:(NSString *)secondTitle
{
    self.isHide = hide;
    
    if (hide) {
        self.secondButton.hidden = YES;
        self.line2.hidden = YES;
    }
    
    self.firstStr = firstTitle;
    self.secondStr = secondTitle;
    
    [self.firstButton setTitle:firstTitle forState:(UIControlStateNormal)];
    [self.secondButton setTitle:secondTitle forState:(UIControlStateNormal)];
}

- (void) clickFirstButtonAction:(UIButton *)fBtn {
//    fBtn.selected = !fBtn.selected;
    
    if (self.communtiySectionHeaderViewFirstButtonBlock) {
        self.communtiySectionHeaderViewFirstButtonBlock();
    }
    
//    if (!self.isHide && fBtn.selected) {
//        self.line2.hidden = YES;
//        self.line1.hidden = NO;
//    }

    
    XJLog(@"111111=====:%@",self.isHide? @"YES":@"NO");
}

- (void)clickSecondButtonAction:(UIButton *)secBtn{
//    secBtn.selected = !secBtn.selected;
    XJLog(@"22222222====:%@",self.isHide? @"YES":@"NO");
//    if (!self.isHide && secBtn.selected) {
//        self.line1.hidden = YES;
//        self.line2.hidden = NO;
//    }

    if (self.communitySectionHeaderViewSecondButtonBlock) {
        self.communitySectionHeaderViewSecondButtonBlock();
    }
}

- (void)lineAnimation {
//    CGRect frame = self.frame;
//    self.yellowLineView.frame = CGRectMake(100, frame.size.width - 10, 20, 2);
//    self.yellowLineView.frame = CGRectMake(100,50 - 10 , 20, 2);
//    [self.bgView addSubview:self.yellowLineView];
}

#pragma mark - 六 问题:masonry 动画

/**
 1.Masonry动画
 2.动画 刷新问题 bug
 */
@end
